﻿using LightWadf.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LightWadf.Route
{
    /// <summary>
    /// String 类型的 RouteItem
    /// </summary>
    class StringRouteItem : RouteItemMeaning
    {
        private string defaultString = null;
        private int minLength = 0;
        private int maxLength = 0;
        private Regex regex = null;

        public StringRouteItem(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                Regex regex = new Regex(@",([dlr])\[([^\]]+)\]");
                MatchCollection matchs = regex.Matches(str);
                foreach (Match match in matchs)
                {
                    switch (match.Groups[1].Value)
                    {
                        case "d":
                            this.defaultString = match.Groups[2].Value;
                            break;
                        case "l":
                            string range = match.Groups[2].Value;
                            string[] len = range.Split(',');
                            if (len.Length != 2)
                            {
                                LogUtil.WritelnLog("string 类型的路由的 length(l) 属性格式应为:l[0,1]，不能有多余符号");
                                throw new TypeInitializationException("LightWadf.Route.StringRouteItem", new Exception("string 类型的路由的 length(l) 属性格式应为:l[0,1]，不能有多余符号"));
                            }
                            else
                            {
                                try
                                {
                                    this.minLength = int.Parse(len[0]);
                                    this.maxLength = int.Parse(len[1]);
                                }
                                catch (Exception e)
                                {
                                    LogUtil.WritelnLog(e.Message);
                                    LogUtil.WritelnLog("在初始化 StringRouteItem 的 length(l) 属性时，无法将 string 转换为 int");
                                    throw new TypeInitializationException("LightWadf.Route.StringRouteItem", new Exception("在初始化 StringRouteItem 的 length(l) 属性时，无法将 string 转换为 int"));
                                }
                            }
                            break;
                        case "r":
                            this.regex = new Regex(match.Groups[2].Value);
                            break;
                    }
                }
            }
        }

        public Tuple<bool, object> Match(string str)
        {
            if (str != null && str.Length >= this.minLength && str.Length <= this.maxLength)
            {
                if (this.regex == null)
                {
                    return Tuple.Create<bool, object>(true, str);
                }
                else if (this.regex.IsMatch(str))
                {
                    return Tuple.Create<bool, object>(true, str);
                }
            }
            if(!string.IsNullOrEmpty(this.defaultString))
                return Tuple.Create<bool, object>(true, this.defaultString);
            else
            {
                return Tuple.Create<bool, object>(false, null);
            }
        }
    }
}
